﻿using ProductMonitor.Models;
using System;
using System.Collections.Generic;
using System.DirectoryServices.ActiveDirectory;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProductMonitor.UserControls
{
    /// <summary>
    /// RaderUC.xaml 的交互逻辑
    /// </summary>
    public partial class RaderUC : UserControl
    {
        public RaderUC()
        {
            InitializeComponent();

            SizeChanged += OnSizeChanged;
        }

        /// <summary>
        /// 窗口大小改变时执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnSizeChanged(object sender, SizeChangedEventArgs e)
        {
            Draw();
        }

        //支持数据绑定
        public List<RaderModel> ItemSource
        {
            get { return (List<RaderModel>)GetValue(ItemSourceProperty); }
            set { SetValue(ItemSourceProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ItemSource.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemSourceProperty =
            DependencyProperty.Register("ItemSource", typeof(List<RaderModel>), typeof(RaderUC));

        /// <summary>
        /// 画图方法
        /// </summary>
        public void Draw() 
        {
            if (ItemSource == null || ItemSource.Count == 0)
            {
                return;
            }
            //清除原内容
            mainCanvas.Children.Clear();
            P1.Points.Clear();
            P2.Points.Clear();
            P3.Points.Clear();
            P4.Points.Clear();
            P5.Points.Clear();

            //调整大小
            double size = Math.Min(RenderSize.Width, RenderSize.Height);
            LayoutRoot.Width = LayoutRoot.Height = size;

            //半径
            double raduis = size / 2;

            //步的跨度
            double step = 360.0 / ItemSource.Count;

            for (int i = 0; i < ItemSource.Count; i++) 
            {
                //X Y坐标
                double x = (raduis - 20) * Math.Cos((step * i - 90) * Math.PI / 180);//x偏移量
                double y = (raduis - 20) * Math.Sin((step * i - 90) * Math.PI / 180);//y偏移量
                P1.Points.Add(new Point(raduis + x, raduis + y));
                P2.Points.Add(new Point(raduis + x * 0.75, raduis + y * 0.75));
                P3.Points.Add(new Point(raduis + x * 0.50, raduis + y * 0.50));
                P4.Points.Add(new Point(raduis + x * 0.25, raduis + y * 0.25));

                //数据多边形
                P5.Points.Add(new Point(raduis + x * ItemSource[i].ItemValue/100, raduis + y * ItemSource[i].ItemValue / 100));

                //文字处理
                TextBlock txt = new()
                {
                    Width = 60,
                    FontSize = 10,
                    TextAlignment = TextAlignment.Center,
                    Text = ItemSource[i].ItemName,
                    Foreground = new SolidColorBrush(Color.FromArgb(100,255,255,255))
                };
                txt.SetValue(Canvas.LeftProperty, raduis + raduis * Math.Cos((step * i - 90) * Math.PI / 180) - 35);//设置左边间距
                txt.SetValue(Canvas.TopProperty,raduis+(raduis-10)*Math.Sin((step*i-90)*Math.PI/180)-7);//设置上边间距

                mainCanvas.Children.Add(txt);
            }
        }

    }
}
